home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / PROGRAMMING / ZIPLIB / c / minigzip < prev    next >
Text File  |  1996-07-31  |  6KB  |  240 lines

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995-1996 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /*
  7.  * minigzip is a minimal implementation of the gzip utility. This is
  8.  * only an example of using zlib and isn't meant to replace the
  9.  * full-featured gzip. No attempt is made to deal with file systems
  10.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  11.  * very limited. So use minigzip only for testing; use gzip for the
  12.  * real thing. On MSDOS, use only on file names without extension
  13.  * or in pipe mode.
  14.  */
  15.  
  16. /* $Id: minigzip.c,v 1.9 1996/05/22 11:52:32 me Exp $ */
  17.  
  18. #include <stdio.h>
  19. #include "zlib.h"
  20.  
  21. #ifdef STDC
  22. #  include <string.h>
  23. #  include <stdlib.h>
  24. #else
  25.    extern void exit  OF((int));
  26. #endif
  27.  
  28.  
  29. #if defined(MSDOS) || defined(OS2) || defined(WIN32)
  30. #  include <fcntl.h>
  31. #  include <io.h>
  32. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  33. #else
  34. #  define SET_BINARY_MODE(file)
  35. #endif
  36.  
  37. #ifdef VMS
  38. #  define unlink delete
  39. #  define GZ_SUFFIX "-gz"
  40. #else
  41. #  define GZ_SUFFIX ".gz"
  42. #endif
  43. #define SUFFIX_LEN sizeof(GZ_SUFFIX)
  44.  
  45. extern int unlink OF((const char *));
  46.  
  47. #define BUFLEN 4096
  48. #define MAX_NAME_LEN 1024
  49.  
  50. #define local static
  51. /* For MSDOS and other systems with limitation on stack size. For Unix,
  52.     #define local
  53.    works also.
  54.  */
  55.  
  56. char *prog;
  57.  
  58. void error           OF((const char *msg));
  59. void gz_compress     OF((FILE   *in, gzFile out));
  60. void gz_uncompress   OF((gzFile in, FILE   *out));
  61. void file_compress   OF((char  *file));
  62. void file_uncompress OF((char  *file));
  63. int  main            OF((int argc, char *argv[]));
  64.  
  65. /* ===========================================================================
  66.  * Display error message and exit
  67.  */
  68. void error(msg)
  69.     const char *msg;
  70. {
  71.     fprintf(stderr, "%s: %s\n", prog, msg);
  72.     exit(1);
  73. }
  74.  
  75. /* ===========================================================================
  76.  * Compress input to output then close both files.
  77.  */
  78. void gz_compress(in, out)
  79.     FILE   *in;
  80.     gzFile out;
  81. {
  82.     local char buf[BUFLEN];
  83.     int len;
  84.     int err;
  85.  
  86.     for (;;) {
  87.         len = fread(buf, 1, sizeof(buf), in);
  88.         if (ferror(in)) {
  89.             perror("fread");
  90.             exit(1);
  91.         }
  92.         if (len == 0) break;
  93.  
  94.         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  95.     }
  96.     fclose(in);
  97.     if (gzclose(out) != Z_OK) error("failed gzclose");
  98. }
  99.  
  100. /* ===========================================================================
  101.  * Uncompress input to output then close both files.
  102.  */
  103. void gz_uncompress(in, out)
  104.     gzFile in;
  105.     FILE   *out;
  106. {
  107.     local char buf[BUFLEN];
  108.     int len;
  109.     int err;
  110.  
  111.     for (;;) {
  112.         len = gzread(in, buf, sizeof(buf));
  113.         if (len < 0) error (gzerror(in, &err));
  114.         if (len == 0) break;
  115.  
  116.         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  117.         error("failed fwrite");
  118.     }
  119.     }
  120.     if (fclose(out)) error("failed fclose");
  121.  
  122.     if (gzclose(in) != Z_OK) error("failed gzclose");
  123. }
  124.  
  125.  
  126. /* ===========================================================================
  127.  * Compress the given file: create a corresponding .gz file and remove the
  128.  * original.
  129.  */
  130. void file_compress(file)
  131.     char  *file;
  132. {
  133.     local char outfile[MAX_NAME_LEN];
  134.     FILE  *in;
  135.     gzFile out;
  136.  
  137.     strcpy(outfile, file);
  138.     strcat(outfile, GZ_SUFFIX);
  139.  
  140.     in = fopen(file, "rb");
  141.     if (in == NULL) {
  142.         perror(file);
  143.         exit(1);
  144.     }
  145.     out = gzopen(outfile, "wb"); /* use "wb9" for maximal compression */
  146.     if (out == NULL) {
  147.         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  148.         exit(1);
  149.     }
  150.     gz_compress(in, out);
  151.  
  152.     unlink(file);
  153. }
  154.  
  155.  
  156. /* ===========================================================================
  157.  * Uncompress the given file and remove the original.
  158.  */
  159. void file_uncompress(file)
  160.     char  *file;
  161. {
  162.     local char buf[MAX_NAME_LEN];
  163.     char *infile, *outfile;
  164.     FILE  *out;
  165.     gzFile in;
  166.     int len = strlen(file);
  167.  
  168.     strcpy(buf, file);
  169.  
  170.     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  171.         infile = file;
  172.         outfile = buf;
  173.         outfile[len-3] = '\0';
  174.     } else {
  175.         outfile = file;
  176.         infile = buf;
  177.         strcat(infile, GZ_SUFFIX);
  178.     }
  179.     in = gzopen(infile, "rb");
  180.     if (in == NULL) {
  181.         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  182.         exit(1);
  183.     }
  184.     out = fopen(outfile, "wb");
  185.     if (out == NULL) {
  186.         perror(file);
  187.         exit(1);
  188.     }
  189.  
  190.     gz_uncompress(in, out);
  191.  
  192.     unlink(infile);
  193. }
  194.  
  195.  
  196. /* ===========================================================================
  197.  * Usage:  minigzip [-d] [files...]
  198.  */
  199.  
  200. int main(argc, argv)
  201.     int argc;
  202.     char *argv[];
  203. {
  204.     int uncompr = 0;
  205.     gzFile file;
  206.  
  207.     prog = argv[0];
  208.     argc--, argv++;
  209.  
  210.     if (argc > 0) {
  211.         uncompr = (strcmp(*argv, "-d") == 0);
  212.         if (uncompr) {
  213.             argc--, argv++;
  214.         }
  215.     }
  216.     if (argc == 0) {
  217.         SET_BINARY_MODE(stdin);
  218.         SET_BINARY_MODE(stdout);
  219.         if (uncompr) {
  220.             file = gzdopen(fileno(stdin), "rb");
  221.             if (file == NULL) error("can't gzdopen stdin");
  222.             gz_uncompress(file, stdout);
  223.         } else {
  224.             file = gzdopen(fileno(stdout), "wb"); /* "wb9" for max compr. */
  225.             if (file == NULL) error("can't gzdopen stdout");
  226.             gz_compress(stdin, file);
  227.         }
  228.     } else {
  229.         do {
  230.             if (uncompr) {
  231.                 file_uncompress(*argv);
  232.             } else {
  233.                 file_compress(*argv);
  234.             }
  235.         } while (argv++, --argc);
  236.     }
  237.     exit(0);
  238.     return 0; /* to avoid warning */
  239. }
  240.